﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace finalexam
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "欢迎来到学生信息管理系统";
        }
        protected void btnRead_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014112134;uid=2014112134;pwd=19951124";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                cn.Open();
                string strNo = this.txtNo.Text;

                string strSql = "SELECT   No, Name,Gender,NativePlace, Age,Birth,College,Class,Memo    FROM  student WHERE   (No = N'" + strNo + "')";


                SqlCommand cmd = new SqlCommand(strSql, cn);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    this.txtAge.Text = dr["Age"].ToString();
                    this.txtCol.Text = dr["College"].ToString();
                    this.txtMemo.Text = dr["Memo"].ToString();
                    this.txtName.Text = dr["Name"].ToString();
                    this.txtGender.Text = dr["Gender"].ToString();
                    this.txtNatPlace.Text = dr["NativePlace"].ToString();
                    this.txtBirth.Text = dr["Birth"].ToString();
                    this.txtClass.Text = dr["Class"].ToString();
                    
                    this.Label6.Text = "";
                }
                else
                {
                    this.Label6.Text = "该学生信息不存在！请输入正确学号！";
                    this.txtAge.Text = "";
                    this.txtCol.Text = "";
                    this.txtMemo.Text = "";
                    this.txtName.Text = "";
                    this.txtGender.Text = "";
                    this.txtNatPlace.Text = "";
                    this.txtBirth.Text = "";
                    this.txtClass.Text ="";
                }

                dr.Close();
            }
            catch (Exception ex)
            {
                this.Label6.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014112134;uid=2014112134;pwd=19951124";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                cn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("UPDATE  student ");
                SqlStringBuilder.AppendFormat("SET Name = N'{0}', Age = {1}, College = N'{2}', Memo = N'{3}',Gender = N'{4}',Birth = N'{5}',Class = N'{6}',NativePlace = N'{7}' ", this.txtName.Text, this.txtAge.Text, this.txtCol.Text, this.txtMemo.Text, this.txtGender.Text, this.txtBirth.Text, this.txtClass.Text, this.txtNatPlace.Text);
                SqlStringBuilder.AppendFormat("WHERE   (No = N'{0}') ", this.txtNo.Text);

                string strSql = SqlStringBuilder.ToString();
                SqlCommand cmd = new SqlCommand(strSql, cn);
                int iUpd = cmd.ExecuteNonQuery();
                if (iUpd < 1)
                {
                    this.Label6.Text = "修改失败！";
                }
                else
                {
                    this.Label6.Text = "修改" + iUpd.ToString() + "条记录！";
                }
            }
            catch (Exception ex)
            {
                this.Label6.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        

        protected void btnRead1_Click(object sender, EventArgs e)
        {
            String strCn = "server=pc.hstc.cn;database=dhd2014112134;uid=2014112134;pwd=19951124";
            SqlConnection cn = new SqlConnection(strCn);
            try          
            {
                cn.Open();
                string strSql = "SELECT   *   FROM  student";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                SqlDataReader dr = cmd.ExecuteReader();
                this.GridView1.DataSource = dr;
                this.GridView1.DataBind();
                dr.Close();
                   
            }
            catch (Exception ex)               
            {
                this.Label6.Text = ex.Message;
            }
            finally    
            {
                cn.Close();
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["Theme"] = this.DropDownList1.SelectedValue.ToString();
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["Theme"] != null)
                this.Theme = Session["Theme"].ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }
    }
}